/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sn.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import sn.metier.Fichier;
import sn.metier.Partage;
import sn.service.NotificationService;
import sn.service.PartageService;

/**
 *
 * @author Pluce
 */
@Controller
public class PartageController {
    
    @Autowired
    private PartageService partageService;
    
    @Autowired
    private NotificationService notificationService;
    
    @RequestMapping("/partage")
    @Transactional
    public String partage(HttpSession session, String[] identifications, String[] destinataires, String message,@RequestParam("fichier") MultipartFile fichier) throws IOException{
        if(session.isNew() || session.getAttribute("user") == null){
            return "indexNonConnecte";
        }
        if(message.isEmpty() && fichier.getSize()==0){
            return "redirect:index.htm";
        }
        System.err.println("_________");
        System.err.println(message);
        System.err.println("_________");
        Long auteur = (Long)session.getAttribute("user");
        
        if(destinataires == null || destinataires.length == 0){
            destinataires = new String[1];
            destinataires[0] = auteur.toString();
        }
        
        ArrayList<Long> dest = new ArrayList<Long>();
        for(String i: destinataires){
            dest.add(Long.parseLong(i));
        }
        
        ArrayList<Long> ident = new ArrayList<Long>();
        
        if(identifications != null){
            for(String i: identifications){
                ident.add(Long.parseLong(i));
            }
        }
        
        Partage p;
        if(fichier.getSize() > 0){
            File file = partageService.sauvegardeFichier(fichier.getInputStream());
            p = partageService.partagerFichierAmis(auteur, message, dest, fichier.getOriginalFilename(), file.getName(),fichier.getContentType(),fichier.getSize());            
        } else {
            p = partageService.partagerFichierAmis(auteur, message, dest, null, null, null, null); 
        }
        
        for(Long idIdent : ident){
            notificationService.identifierPersonne(auteur, idIdent, p.getId());
        }
        
        return "redirect:index.htm";
    }
    
    @RequestMapping("/download")
    public void download(HttpSession session, HttpServletResponse response,@RequestParam Long id) throws IOException{
        Fichier f = partageService.chargerFichier(id);
        response.setContentType(f.getType());
        response.setHeader("Content-Disposition","attachment; filename=\"" + f.getNomOriginal()+"\"");
        IOUtils.copy(partageService.chargerFluxFichier(id), response.getOutputStream());
        response.flushBuffer();
    }
    
    @Transactional
    @RequestMapping(value="/supprimer_partage",method=RequestMethod.GET)
    public String lireNoteIdentification(Model model,HttpSession session,@RequestParam Long id){
        if(session.isNew() || session.getAttribute("user") == null){
            return "indexNonConnecte";
        }
        Long auteur = (Long)session.getAttribute("user");

        partageService.supprimerPartage(auteur, id);
        return "redirect:index.htm";
    }
}
